<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加收货地址</title>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1" />
<link rel="stylesheet" href="{{ url('/common/mobile-common/common.css') }}">	
<link rel="stylesheet" href="{{ url('mobile/css/member/addaddress.css') }}">	
<script src="{{ url('mobile/js/jquery-1.8.3.min.js') }}"></script>
<script src="{{ url('/common/mobile-common/hmt.js') }}"></script>
</head>
<body>
	<div class="abouts">
		<div class="wrap-cont clearfix">
				<!--添加新地址-->
				<div class="wrap-cont address-add">
					<div class="secti add-ys">
						<p>
							<span>收货人:</span>
							<input type="text" name="nameUser">
						</p>
						<p>
							<span>联系电话:</span>
							<input type="text" name="telNum">
						</p>
						<p class="areas">
							<span>所在省:</span>
							<select class="province" name="province" required>
								<option value="0">请选择所在省</option>
								@foreach($areas as $area)
									<option city="{{ collect($area['city'])->toJson() }}">{{ $area['name'] }}</option>
								@endforeach
							</select>
						</p>
						<p class="stres">
							<span>所在市:</span>
							<select class="city" name="city">
								<option value="0">请选择所在市</option>
							</select>
						</p>
						<p class="addressTxt">
							<span>详细地址:</span>
							<input type="text" name="addRess">
						</p>
					</div>
					<div class="secti set-mr">
						<p>
							<span>设为默认:</span>
							<em><input type="checkbox" name="sequence" checked="checked">
                                 <img src="{{ url('mobile/images/check.png') }}">
							</em>

						</p>
					</div>
					<div class="secti opt-btn">
						<a class="save-btn">保存</a>
					</div>
				</div>
		</div>
	</div>
</body>
<script>
	$(function(){
		//地区选择
		function getCity() {
			var city = $('.province').find('option:selected').attr('city');
			city = JSON.parse(city);
			var option = '';
			$('.city').empty();
			$(city).each(function () {
				option += '<option>' + this.name + '</option>';
			});
			$('.city').append(option);
		}

		$('.province').on('change',function(){
			getCity();
		});

		//添加地址页面新建地址保存
		$(".save-btn").unbind().click(function(){
			var myreg = /^1[34578]\d{9}$/;
			var nameuser=$(".add-ys p input[name='nameUser']").val();
			var telNum=$(".add-ys p input[name='telNum']").val();
			var province=$(".province option:selected").val();
			var city=$(".city option:selected").val();
			var addRess=$(".add-ys p input[name='addRess']").val();
			if ($("[name=sequence]").is(':checked')) {
				var sequence = 1;
			} else {
				var sequence = 2;
			}

			if(nameuser==''){
				alert("请输入姓名");	
				$(".add-ys p input[name='nameUser']").focus();			
			}
			else if(telNum==''){
				alert("请输入手机号码");
				$(".add-ys p input[name='telNum']").focus();
			}
			else if(!telNum.match(myreg)){
				alert("手机格式不正确！");
				$(".add-ys p input[name='telNum']").focus();
			}
			else if(province==0){
				alert("请选择省");
			}
			else if(city==0){
				alert("请选择市");
			}
			else if(addRess==''){
				alert("请填写详细地址");
				$(".add-ys p input[name='addRess']").focus();
			} else {
				$.ajax({
					type:'post',
					url:"{{ url('wap/receiver/add') }}",
					data:{name:nameuser,phone:telNum,province:province,city:city,address:addRess,sequence:sequence},
					success:function(data){
						if (data.type==1) {
							window.location.href="{{url('wap/receiver')}}";
						} else if (data.type==2) {
							window.location.href="{{url('wap/receiver/index')}}";
						}
					}
				});
			}
		});
    
    //复选框样式
     $("input[type='checkbox']").click(function(){
     		var checkStatus=($(this).attr("checked"));
         	if(checkStatus=='checked'){
         		$(this).parent().find("img").attr("src","{{ url('mobile/images/check.png') }}");
         	}
         	else
         	{
         		$(this).parent().find("img").attr("src","{{ url('mobile/images/no-check.png') }}");
         	}
     });
	});
</script>
</html>
